package com.java.test.system.depositsettings;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.test.dto.PageResultDTO;
import com.java.test.dto.ResponseDTO;
import com.java.test.system.depositsettings.constant.DepositSettingsCodeConst;
import com.java.test.system.depositsettings.dto.DepositSettingsAddDTO;
import com.java.test.system.depositsettings.dto.DepositSettingsQueryDTO;
import com.java.test.system.depositsettings.dto.DepositSettingsUpdateDTO;
import com.java.test.system.depositsettings.entity.DepositSettingsEntity;
import com.java.test.system.depositsettings.vo.DepositSettingsVO;
import com.java.test.system.registrationtemplate.RegistrationTemplateDAO;
import com.java.test.system.registrationtemplate.entity.AreaEntity;
import com.java.test.util.SmartBeanUtil;
import com.java.test.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 打码设置Service
 *
 * @author Administrator
 * @date 2020/6/29 0029
 */
@Service
public class DepositSettingsService {
    @Autowired
    private DepositSettingsDAO depositSettingsDAO;

    @Autowired
    private RegistrationTemplateDAO registrationTemplateDAO;

    /**
     * 查询
     * @param queryDTO
     * @author 
     * @date 2020/7/15 0015 13:45 
     * @return com.java.test.dto.ResponseDTO<com.java.test.dto.PageResultDTO < com.java.test.system.depositsettings.vo.DepositSettingsVO>>
     */
    public ResponseDTO<PageResultDTO<DepositSettingsVO>> getSelectByList(DepositSettingsQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<DepositSettingsEntity> entityList = depositSettingsDAO.getSelectByList(queryDTO);
        for (DepositSettingsEntity entity:entityList) {
            String areaName = " ";
            List<AreaEntity> areaEntityList = registrationTemplateDAO.getAreaListByIds(entity.getAreaId());
            for (AreaEntity q:areaEntityList) {
                areaName+=q.getName()+",";
            }
            entity.setAreaName(areaName.substring(0,areaName.length()-1));
        }
        PageResultDTO<DepositSettingsVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, entityList, DepositSettingsVO.class);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 保存
     * @param addDTO
     * @author 
     * @date 2020/7/15 0015 13:45 
     * @return com.java.test.dto.ResponseDTO<java.lang.String>
     */
    public ResponseDTO<String> save(DepositSettingsAddDTO addDTO) {
        DepositSettingsEntity entity = depositSettingsDAO.getByGName(addDTO);
        if (entity != null) {
            return ResponseDTO.wrap(DepositSettingsCodeConst.ALREADY_EXIST);
        }
        depositSettingsDAO.insertSave(addDTO);
        return ResponseDTO.succ();
    }

    /**
     * 通过id查询
     * @param id
     * @author 
     * @date 2020/7/15 0015 13:45 
     * @return com.java.test.dto.ResponseDTO<com.java.test.system.depositsettings.vo.DepositSettingsVO>
     */
    public ResponseDTO<DepositSettingsVO> selectById(String id) {
        DepositSettingsEntity entity = depositSettingsDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(DepositSettingsCodeConst.NOT_EXIST);
        }
        String areaName = " ";
        List<AreaEntity> areaEntityList = registrationTemplateDAO.getAreaListByIds(entity.getAreaId());
        for (AreaEntity q:areaEntityList) {
            areaName+=q.getName()+",";
        }
        entity.setAreaName(areaName.substring(0,areaName.length()-1));
        DepositSettingsVO configDTO = SmartBeanUtil.copy(entity, DepositSettingsVO.class);
        return ResponseDTO.succData(configDTO);
    }

    /**
     * 修改
     * @param updateDTO
     * @author 
     * @date 2020/7/15 0015 13:45 
     * @return com.java.test.dto.ResponseDTO<java.lang.String>
     */
    public ResponseDTO<String> updateById(DepositSettingsUpdateDTO updateDTO) {
        DepositSettingsEntity entity = depositSettingsDAO.getById(updateDTO.getId());
        if (entity == null) {
            return ResponseDTO.wrap(DepositSettingsCodeConst.NOT_EXIST);
        }
        DepositSettingsEntity alreadyEntity = depositSettingsDAO.getByKeyExcludeId(updateDTO);
        if (alreadyEntity != null) {
            return ResponseDTO.wrap(DepositSettingsCodeConst.ALREADY_EXIST);
        }
        depositSettingsDAO.updateById(updateDTO);
        return ResponseDTO.succ();
    }
    
    /**
     * 删除
     * @param id
     * @author 
     * @date 2020/7/15 0015 13:45 
     * @return com.java.test.dto.ResponseDTO<java.lang.String>
     */
    public ResponseDTO<String> deleteById(String id) {
        DepositSettingsEntity entity = depositSettingsDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(DepositSettingsCodeConst.NOT_EXIST);
        }
        depositSettingsDAO.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * 按照充值金额查询打码量
     * @param amount
     * @author lijie1
     * @date 2020/8/4 0004 9:33
     * @return com.java.test.system.depositsettings.entity.DepositSettingsEntity
     */
    public DepositSettingsEntity selectSettingByAmount(Double amount){
        List<DepositSettingsEntity> list = depositSettingsDAO.selectSettingByAmount(amount);
        DepositSettingsEntity info = new DepositSettingsEntity();
        if (list.size()>0){
            info = list.get(0);
        }
        return info;
    }
}
